package com.smart.village.big.screen.controller.micro;

import com.google.common.collect.Lists;
import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

import static com.smart.village.big.screen.domain.AjaxResult.error;
import static com.smart.village.big.screen.domain.AjaxResult.success;


/**
 * 小程序-新闻类-管理员视角
 */
@Tag(name = "小程序-新闻类数据接口-管理员视角")
@RestController
@RequestMapping("/micro/manager/news")
public class MicroNewsManagerController {

    Logger logger = Logger.getLogger(MicroNewsManagerController.class.getName());

    @Autowired
    private NewsService newsService;


    /**
     * 小程序 新闻列表 根据新闻序列号 获取详情
     *
     * @param newsSerialNumber
     * @return
     */
    @Operation(summary = "小程序管理员视角 新闻列表 根据新闻序列号 获取详情")
    @GetMapping("/selectByPrimaryKey/{newsSerialNumber}")
    @ResponseBody
    public AjaxResult selectByPrimaryKey(@PathVariable("newsSerialNumber") @Parameter(description = "新闻序列号") String newsSerialNumber) {
        try {
            return success(newsService.selectByPrimaryKey(newsSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序管理员视角 党员事务  新闻审核列表
     *
     * @return
     */
    @Operation(summary = "小程序管理员视角 党员事务  新闻审核列表")
    @GetMapping("/getAuditList")
    @ResponseBody
    public AjaxResult getAuditList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam @Parameter(description = "组织id") String partyOrganizationSerialNumber,
            @RequestParam(required = false,defaultValue = "") @Parameter(name = "auditStatus", description = "审核状态") String auditStatus,
            @RequestParam(required = false,defaultValue = "") @Parameter(name = "activityType", description = "活动类别") String activityType,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            List<String> activityTypeList = Lists.newArrayList();
            if (StringUtils.isEmpty(activityType)) {
                activityTypeList.add("党员大会");
                activityTypeList.add("党小组会");
                activityTypeList.add("支委会");
                activityTypeList.add("党课");
                activityTypeList.add("双联系双报到");
                activityTypeList.add("主题党日");
                activityTypeList.add("民主生活会");
                activityTypeList.add("组织生活会");
            } else {
                if (activityType.equals("党员大会")) {
                    activityTypeList.add("党员大会");
                }
                if (activityType.equals("党小组会")) {
                    activityTypeList.add("党小组会");
                }
                if (activityType.equals("支委会")) {
                    activityTypeList.add("支委会");
                }
                if (activityType.equals("党课")) {
                    activityTypeList.add("党课");
                }
                if (activityType.equals("双联系双报到")) {
                    activityTypeList.add("双联系双报到");
                }
                if (activityType.equals("主题党日")) {
                    activityTypeList.add("主题党日");
                }
                if (activityType.equals("民主生活会")) {
                    activityTypeList.add("民主生活会");
                }
                if (activityType.equals("组织生活会")) {
                    activityTypeList.add("组织生活会");
                }
            }

            switch (auditStatus) {
                case "":
                    auditStatus = "";
                    break;
                case "待审核":
                    auditStatus = "2";
                    break;
                case "审核通过":
                    auditStatus = "0";
                    break;
                case "审核驳回":
                    auditStatus = "3";
                    break;
                default:
                    auditStatus = "";
                    break;
            }
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getAuditList(search, partyOrganizationSerialNumber, auditStatus, activityTypeList, "支部", pageNum, pageSize,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 小程序管理员视角 党建事务  党建资讯
     * @param newsCategorySerialNumber
     * @param search
     * @param phone
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Operation(summary = "小程序管理员视角 党建事务  党建资讯")
    @GetMapping("/getList")
    @ResponseBody
    public AjaxResult getList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "主分类序列号") String newsCategorySerialNumber,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam @Parameter(description = "用户id") String phone,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            return success(newsService.getMicroManagerPartyNewsList(newsCategorySerialNumber,phone, pageNum, pageSize,search,1,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


}
